// choose.js - 闯关模式难度选择页面功能

let selectedDifficulty = null;

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    initializeDifficultyCards();
    setupEventListeners();
});

// 初始化难度卡片
function initializeDifficultyCards() {
    const cards = document.querySelectorAll('.difficulty-card');
    
    cards.forEach(card => {
        card.addEventListener('click', function() {
            selectDifficulty(this);
        });
    });
}

// 设置事件监听器
function setupEventListeners() {
    // 选择按钮点击事件
    const selectButtons = document.querySelectorAll('.select-btn');
    selectButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            e.stopPropagation();
            const card = this.closest('.difficulty-card');
            selectDifficulty(card);
            confirmSelection();
        });
    });
    
    // 返回按钮点击事件
    const backBtn = document.querySelector('.back-btn');
    if (backBtn) {
        backBtn.addEventListener('click', function(e) {
            e.preventDefault();
            goBack();
        });
    }
}

// 选择难度
function selectDifficulty(card) {
    // 移除之前的选择状态
    const cards = document.querySelectorAll('.difficulty-card');
    cards.forEach(c => c.classList.remove('selected'));
    
    // 添加当前选择状态
    card.classList.add('selected');
    
    // 更新选择的难度
    selectedDifficulty = card.dataset.difficulty;
    
    // 更新难度信息显示
    updateDifficultyInfo();
}

// 更新难度信息显示
function updateDifficultyInfo() {
    const infoTitle = document.querySelector('.info-title');
    const infoContent = document.querySelector('.info-content');
    
    if (!selectedDifficulty) {
        infoTitle.textContent = '请选择难度';
        infoContent.textContent = '点击任意难度卡片查看详细信息';
        return;
    }
    
    const difficultyInfo = {
        'easy': {
            title: '简单模式',
            description: '适合初次体验的玩家，蛇移动速度较慢，食物出现频率高，轻松享受游戏乐趣。'
        },
        'medium': {
            title: '中等模式',
            description: '适合有一定经验的玩家，蛇移动速度适中，食物和障碍物分布均衡，挑战与乐趣并存。'
        },
        'hard': {
            title: '困难模式',
            description: '适合高手玩家，蛇移动速度快，食物稀少，障碍物密集，考验你的反应和策略能力。'
        }
    };
    
    const info = difficultyInfo[selectedDifficulty];
    if (info) {
        infoTitle.textContent = info.title;
        infoContent.textContent = info.description;
    }
}

// 确认选择
function confirmSelection() {
    if (!selectedDifficulty) {
        showMessage('请先选择一个难度！', 'warning');
        return;
    }
    
    const confirmMessage = `确定选择${getDifficultyName(selectedDifficulty)}吗？`;
    
    if (confirm(confirmMessage)) {
        // 保存难度选择到本地存储
        localStorage.setItem('selectedDifficulty', selectedDifficulty);
        
        // 跳转到游戏页面
        window.location.href = 'challenge-game.html';
    }
}

// 获取难度名称
    function getDifficultyName(difficulty) {
        const names = {
            'easy': '简单模式',
            'medium': '中等模式',
            'hard': '困难模式'
        };
        return names[difficulty] || '未知难度';
    }

// 显示消息
function showMessage(message, type = 'info') {
    // 创建消息元素
    const messageDiv = document.createElement('div');
    messageDiv.className = `alert alert-${type} alert-dismissible fade show`;
    messageDiv.innerHTML = `
        ${message}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    `;
    
    // 添加到页面
    const container = document.querySelector('.difficulty-container');
    container.insertBefore(messageDiv, container.firstChild);
    
    // 3秒后自动消失
    setTimeout(() => {
        if (messageDiv.parentNode) {
            messageDiv.remove();
        }
    }, 3000);
}

// 返回上一页
function goBack() {
    window.history.back();
}

// 键盘快捷键支持
document.addEventListener('keydown', function(e) {
    switch(e.key) {
        case '1':
            selectDifficulty(document.querySelector('.difficulty-card.easy'));
            break;
        case '2':
            selectDifficulty(document.querySelector('.difficulty-card.medium'));
            break;
        case '3':
            selectDifficulty(document.querySelector('.difficulty-card.hard'));
            break;
        case 'Enter':
            confirmSelection();
            break;
        case 'Escape':
            goBack();
            break;
    }
});